package com.zz.apk.utils

import com.zz.framework.core.ext.getInt

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created by zanezhao on 2020-02-22 15:41.
 *
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
object VersionUtils {
    /**
     * 比较版本号的大小
     * 前比后：大于返回正数， 相等返回0， 小于返回负数
     * @param mVersionString1
     * @param mVersionString2
     * @return
     */
//    fun compareVersion(mVersionString1: String?, mVersionString2: String?): Int {
//        loge("mVersionString1:$mVersionString1       mVersionString2:$mVersionString2")
//        if (mVersionString1 == null && mVersionString2 == null)
//            return 0
//        else if (mVersionString1 == null)
//            return -1
//        else if (mVersionString2 == null)
//            return 1
//        val arr1 =
//            mVersionString1.split("[^a-zA-Z0-9]+").toTypedArray()
//        val arr2 =
//            mVersionString2.split("[^a-zA-Z0-9]+").toTypedArray()
//        var mIntNumber1: Int
//        var mIntNumber2: Int
//        var mIntNumber3: Int
//        var index = 0
//        val max = Math.min(arr1.size, arr2.size)
//        while (index <= max) {
//            if (index == arr1.size) return if (index == arr2.size) 0 else -1 else if (index == arr2.size) return 1
//            mIntNumber1 = try {
//                arr1[index].toInt()
//            } catch (e: Exception) {
//                Int.MAX_VALUE
//            }
//            mIntNumber2 = try {
//                arr2[index].toInt()
//            } catch (e: Exception) {
//                Int.MAX_VALUE
//            }
//            if (mIntNumber1 != mIntNumber2) {
//                return mIntNumber1 - mIntNumber2
//            }
//            mIntNumber3 = arr1[index].compareTo(arr2[index])
//            if (mIntNumber3 != 0) return mIntNumber3
//            index++
//        }
//        return 0
//    }


    /**
     * 比较版本名称大小 前比后：大于返回正数， 相等返回0， 小于返回负数
     * @param version1
     * @param version2
     * @return version1 < version2 -1 </br>  version1 > version2 1
     */
    fun compareVersion(version1: String?, version2: String?): Int {
        var version1: String? = version1
        var version2: String? = version2
        if (version1 == null || version2 == null) {
            return 0
        }
        version1 = version1.trim().toLowerCase()
        version2 = version2.trim().toLowerCase()
        version1 = version1.replace("v", "")
        version1 = version1.replace("_beta", "")
        version2 = version2.replace("v", "")
        version2 = version2.replace("_beta", "")
        val versionArray1 = version1.split("\\.".toRegex()).toTypedArray() // 注意此处为特殊字符，需要转义
        val versionArray2 = version2.split("\\.".toRegex()).toTypedArray() // 注意此处为特殊字符，需要转义
        val maxSizeArray = if (versionArray1.size>versionArray2.size){
            versionArray1
        }else{
            versionArray2
        }
        for (i in maxSizeArray.indices) {
            var sum = versionArray1.getOrNull(i).getInt() - versionArray2.getOrNull(i).getInt()
            if (sum > 0) {
                return 1
            } else if (sum < 0) {
                return -1
            }
        }
        return 0
    }

    @JvmStatic
    fun main(args: Array<String>) {
        println(compareVersion("2.1.10", "2.1.9"))
    }
}